import { IMOptionsType } from "@/types/dict";
import { handleAction, refreshTable } from ".";
import {
  columns,
  tableActiveKey,
  defaultSearchList,
  defaultColumns,
  resetFormState,
  drawerFormState,
  tabPower,
} from "./store";
import { getSelfNodeIcon } from "@system/menu/store";
const defineApiMap = (searchParams, format, searchKey) => {
  const getTimeArray = (num) =>
    toLocalTime(searchParams?.[searchKey], format)[num];
  return new Map([[/^[a-zA-Z_0-9]{1,}[0-1]$/, getTimeArray]]);
};
export const handleSearchTime = (
  array,
  searchParams = {},
  format = "YYYY-MM-DD HH:mm:ss",
  searchKey = "start_end"
) => {
  const stringArray: string[] = [];
  for (let i = 0; i < array.length; i++) {
    const string = array[i];
    let resultArray = [...defineApiMap(searchParams, format, searchKey)].filter(
      ([key]) => key.test(`${string}${i}`)
    );
    const timeArray = resultArray.map(([, api]) => api.call(this, i));
    const tmp = timeArray[0];
    stringArray[i] = tmp;
    searchParams[string] = tmp;
  }
  delete searchParams[searchKey];
  return stringArray;
};
export const handleSearchLoading = (
  boolean,
  type,
  { searchLoading, resetLoading }
) => {
  const set_searchLoading = (value) => (searchLoading.value = value);
  const set_resetLoading = (value) => (resetLoading.value = value);
  const defineApiMap = new Map([
    [0, set_searchLoading],
    [1, set_resetLoading],
  ]);
  const api = defineApiMap.get(type) as (value: any) => any;
  api(boolean);
};
export const searchList = ref<any>(defaultSearchList[tableActiveKey.value]);
const searchLoading = ref(false);
const resetLoading = ref(false);
export const searchConfig = () => {
  const defineSearch = {
    okText: getProxyT("base.query"),
    cancelText: getProxyT("base.reset"),
    searchLoading,
    resetLoading,
    refreshTableData: async (searchParams: any, type: any) => {
      if (tableActiveKey.value === 0)
        handleSearchTime(["start_date", "end_date"], searchParams);
      if (tableActiveKey.value === 1)
        handleSearchTime(["start_created_at", "end_created_at"], searchParams);
      handleSearchLoading(true, type, defineSearch);
      await refreshTable(searchParams);
      handleSearchLoading(false, type, defineSearch);
    },
    extra: () => {
      return (
        <>
          {tableActiveKey.value === 0 ? (
            <>
              {withDirectivePower({
                vNode: (
                  <s-button
                    type="primary"
                    onClick={() => handleAction({ type: "add" })}
                    icon={renderIcon(getSelfNodeIcon("userApi.createPerson"))}
                  >
                    {$t("base.addUser")}
                  </s-button>
                ),
                bindingValue: "userApi.createPerson",
              })}
            </>
          ) : null}
        </>
      );
    },
    tabsSlot: {
      activeKey: tableActiveKey,
      on: {
        change(tableActiveKey: any) {
          searchList.value = defaultSearchList[tableActiveKey];
          initObjAttr(columns, defaultColumns[tableActiveKey]);
          resetFormState(drawerFormState);
          refreshTable();
        },
      },
      options: ([] as IMOptionsType[])
        .concat(
          tabPower[0]
            ? {
                label: getProxyT("user4"),
                value: 0,
              }
            : []
        )
        .concat(
          tabPower[1]
            ? {
                label: getProxyT("user5"),
                value: 1,
              }
            : []
        ),
    },
    searchList,
  };
  return defineSearch;
};
